package com.nageoffer.shortlink.admin.remote.dto;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nageoffer.shortlink.admin.common.convention.result.Result;
import com.nageoffer.shortlink.admin.remote.dto.req.ShortLinkCreateReqDTO;
import com.nageoffer.shortlink.admin.remote.dto.req.ShortLinkPageReqDTO;
import com.nageoffer.shortlink.admin.remote.dto.resp.ShortLinkCreateRespDTO;
import com.nageoffer.shortlink.admin.remote.dto.resp.ShortLinkPageRespDTO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.Map;

/*
* 短链接中台远程调用服务
* */
public interface ShortLinkRemoteService {

    /*
    * 创建短链接
    * */
    default  Result<ShortLinkCreateRespDTO> createShortLink( ShortLinkCreateReqDTO shortLinkCreateReqDTO) {
         String resultBodyStr=HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/create",JSON.toJSONString(shortLinkCreateReqDTO));
         return JSON.parseObject(resultBodyStr, new TypeReference<>() {
         });
    }

    /*
    * 短链接分页查询
    * */
    default Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO shortLinkPageReqDTO){
        Map<String,Object> map = new HashMap<>();
        map.put("gid",shortLinkPageReqDTO.getGid());
        map.put("current",shortLinkPageReqDTO.getCurrent());
        map.put("size",shortLinkPageReqDTO.getSize());
        String resultPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/page",map);

        return JSON.parseObject(resultPageStr, new TypeReference<>() {
        });
    }
}
